package org.haifi.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.haifi.game.GameException;
import org.haifi.game.GameString;
import org.haifi.game.annotation.GString;
import org.junit.Test;

import com.google.common.collect.Lists;

/**
 * 用于重新生成excel翻译文档
 * 
 * 因为testcase是在工程中执行，所以不需要使用classloader加载，仅使用相对路径即可
 * 
 * @author joycube2
 * 
 */
public class XlsLanguageGenerateUtil extends GameBaseManagerTestCase {
	@Test
	public void generateXlsLanguage() {
		try {
			Class clazz = GameException.class;
			// testcase运行目录是工程所在目录
			String xlsName = "src/main/resources/xls/language.xls";

			File file = new File(xlsName);
			log.info(file.exists());

			FileInputStream fileInputStream = new FileInputStream(new File(
					xlsName));
			HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);

			// 处理异常
			processSheet(workbook, "exception", GameException.class);
			// 处理游戏中的字符串
			processSheet(workbook, "string", GameString.class);

			// 保存至文件
			FileOutputStream fileOutputStream = new FileOutputStream(new File(
					xlsName));
			workbook.write(fileOutputStream);
		} catch (Exception e) {
			log.error("ERROR", e);
		}
	}

	private void processSheet(HSSFWorkbook workbook, String sheetName,
			Class clazz) throws Exception {
		HSSFSheet sheet = workbook.getSheet(sheetName);
		String[] titles = parseCell(sheet.getRow(0));
		List<String[]> allDatas = Lists.newArrayList();
		for (int m = 1; m <= sheet.getLastRowNum(); m++) {
			String[] data = parseCell(sheet.getRow(m));
			allDatas.add(data);
		}

		// 检测gameException中所有的属性是否全部在excel中。
		for (Field field : clazz.getFields()) {
			GString error = field.getAnnotation(GString.class);
			if (error != null) {
				boolean exists = false;

				String key = field.getName();
				String value = error.value();
				for (String[] data : allDatas) {
					if (key.equals(data[0])) {
						exists = true;
						data[1] = value;
						break;
					}
				}
				if (!exists) {
					allDatas.add(new String[] { key, value });
				}
			}
		}

		Collections.sort(allDatas, new Comparator<String[]>() {
			@Override
			public int compare(String[] o1, String[] o2) {
				return o1[0].compareTo(o2[0]);
			}
		});

		// 重新输出到xls中。

		// 先清空内容
		for (int m = 0; m < sheet.getLastRowNum(); m++) {
			sheet.removeRow(sheet.getRow(m));
		}

		addRow(sheet, titles, 0);
		for (int m = 0; m < allDatas.size(); m++) {
			String[] data = allDatas.get(m);
			addRow(sheet, data, m + 1);
		}

	}

	private void addRow(HSSFSheet sheet, String[] datas, int rowNum) {
		HSSFRow row = sheet.createRow(rowNum);
		for (int m = 0; m < datas.length; m++) {
			HSSFCell cell = row.createCell(m, HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue(datas[m]);
		}
	}

	private String[] parseCell(HSSFRow row) {
		String[] results = new String[row.getLastCellNum()];
		for (int m = 0; m < results.length; m++) {
			results[m] = parseCellText(row, m);
		}
		return results;
	}

	private String parseCellText(HSSFRow row, int cellNum) {
		String text = "";
		HSSFCell cell = row.getCell(cellNum);
		text = cell.getStringCellValue();
		return text;
	}

}
